{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-08T02:00:56.852956Z",
     "start_time": "2025-09-08T02:00:56.831024Z"
    }
   },
   "source": [
    "import torch\n",
    "\n",
    "X = torch.normal(0, 1, size=(1000, 2))\n",
    "true_w = torch.tensor([2, -3.4])\n",
    "true_b = torch.tensor([4.2])\n",
    "Y = torch.matmul(X, true_w) + true_b\n",
    "epsilon = torch.normal(0, 0.01, size=Y.shape)\n",
    "Y += epsilon\n",
    "Y.reshape((-1, 1))"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 5.9052e+00],\n",
       "        [ 6.6025e+00],\n",
       "        [ 5.5531e+00],\n",
       "        [ 5.3702e+00],\n",
       "        [ 7.1216e+00],\n",
       "        [ 1.5738e-01],\n",
       "        [ 2.5358e+00],\n",
       "        [ 7.6711e-01],\n",
       "        [ 6.2978e+00],\n",
       "        [-2.2905e+00],\n",
       "        [ 8.8581e+00],\n",
       "        [ 4.6006e+00],\n",
       "        [ 9.1297e+00],\n",
       "        [ 9.4475e+00],\n",
       "        [ 1.6832e+00],\n",
       "        [ 1.1147e+00],\n",
       "        [ 7.3949e+00],\n",
       "        [ 2.8142e+00],\n",
       "        [ 5.2353e+00],\n",
       "        [ 3.2652e+00],\n",
       "        [-1.5259e+00],\n",
       "        [ 8.6048e+00],\n",
       "        [-2.4708e+00],\n",
       "        [-2.7148e+00],\n",
       "        [ 1.1714e+01],\n",
       "        [ 5.4101e+00],\n",
       "        [ 1.2654e+01],\n",
       "        [ 4.5979e-01],\n",
       "        [ 7.5525e+00],\n",
       "        [ 3.4243e+00],\n",
       "        [ 3.8825e+00],\n",
       "        [ 9.0739e+00],\n",
       "        [ 8.6477e-01],\n",
       "        [ 5.9490e+00],\n",
       "        [-4.7712e-01],\n",
       "        [-5.1942e+00],\n",
       "        [ 7.1181e+00],\n",
       "        [ 3.0907e+00],\n",
       "        [ 3.7779e+00],\n",
       "        [ 1.1394e+01],\n",
       "        [ 3.4904e+00],\n",
       "        [-4.7333e-01],\n",
       "        [ 8.0442e+00],\n",
       "        [ 1.5681e+00],\n",
       "        [ 5.6051e+00],\n",
       "        [ 6.1382e+00],\n",
       "        [ 5.7906e+00],\n",
       "        [ 6.5328e+00],\n",
       "        [ 5.9134e+00],\n",
       "        [ 8.2060e+00],\n",
       "        [ 4.1206e+00],\n",
       "        [ 9.4692e+00],\n",
       "        [-5.9949e+00],\n",
       "        [ 1.3576e+00],\n",
       "        [ 5.2644e+00],\n",
       "        [ 4.1435e-01],\n",
       "        [ 2.8138e+00],\n",
       "        [ 2.6991e+00],\n",
       "        [ 2.7744e+00],\n",
       "        [-7.5389e-01],\n",
       "        [ 6.3140e+00],\n",
       "        [ 4.8694e+00],\n",
       "        [ 5.1292e+00],\n",
       "        [ 1.5528e+00],\n",
       "        [ 6.3220e+00],\n",
       "        [ 6.8076e-01],\n",
       "        [ 1.1049e+01],\n",
       "        [ 5.7009e+00],\n",
       "        [-1.9224e+00],\n",
       "        [ 3.8251e-01],\n",
       "        [ 5.4571e+00],\n",
       "        [ 2.2691e+00],\n",
       "        [ 6.9171e+00],\n",
       "        [ 7.1812e+00],\n",
       "        [ 4.0750e+00],\n",
       "        [ 3.1982e+00],\n",
       "        [-5.6908e-01],\n",
       "        [ 2.9031e-01],\n",
       "        [-3.3783e+00],\n",
       "        [ 7.7916e+00],\n",
       "        [-2.1491e+00],\n",
       "        [ 8.8583e+00],\n",
       "        [-1.4954e+00],\n",
       "        [ 4.7402e-01],\n",
       "        [ 1.1918e+01],\n",
       "        [ 1.4056e+01],\n",
       "        [ 6.0547e+00],\n",
       "        [-3.4239e+00],\n",
       "        [-6.1037e+00],\n",
       "        [ 5.9905e+00],\n",
       "        [ 1.2039e+01],\n",
       "        [-3.9509e+00],\n",
       "        [ 6.0358e+00],\n",
       "        [-1.4964e+00],\n",
       "        [ 8.5789e+00],\n",
       "        [-4.2863e+00],\n",
       "        [ 9.2066e+00],\n",
       "        [-2.2035e+00],\n",
       "        [ 5.6090e+00],\n",
       "        [ 3.8511e+00],\n",
       "        [ 3.7420e+00],\n",
       "        [ 3.2314e+00],\n",
       "        [ 6.5729e+00],\n",
       "        [ 2.4837e+00],\n",
       "        [ 1.7817e+00],\n",
       "        [ 9.2087e+00],\n",
       "        [ 3.3906e-01],\n",
       "        [-8.1510e-02],\n",
       "        [ 5.0185e+00],\n",
       "        [ 8.4850e+00],\n",
       "        [ 7.8674e-01],\n",
       "        [ 3.6252e+00],\n",
       "        [-3.3731e+00],\n",
       "        [ 5.2535e+00],\n",
       "        [ 8.0836e+00],\n",
       "        [ 8.9023e+00],\n",
       "        [-1.5180e-01],\n",
       "        [ 4.2417e+00],\n",
       "        [ 6.5671e+00],\n",
       "        [ 9.0897e+00],\n",
       "        [ 1.0050e+01],\n",
       "        [ 4.6286e+00],\n",
       "        [ 1.4510e+00],\n",
       "        [ 1.7981e+00],\n",
       "        [ 5.3197e+00],\n",
       "        [ 7.2496e+00],\n",
       "        [ 5.3838e+00],\n",
       "        [ 7.8272e+00],\n",
       "        [ 1.7771e+00],\n",
       "        [ 2.8562e+00],\n",
       "        [ 7.7528e+00],\n",
       "        [ 4.1510e+00],\n",
       "        [ 2.5528e+00],\n",
       "        [ 6.8023e-02],\n",
       "        [ 1.0418e+01],\n",
       "        [ 1.3104e+01],\n",
       "        [ 9.5601e+00],\n",
       "        [ 5.6808e+00],\n",
       "        [ 3.1377e+00],\n",
       "        [ 9.8762e+00],\n",
       "        [ 6.8326e+00],\n",
       "        [ 7.5639e+00],\n",
       "        [ 7.4980e+00],\n",
       "        [ 6.1305e+00],\n",
       "        [ 1.6669e+00],\n",
       "        [ 5.4651e+00],\n",
       "        [-1.7725e+00],\n",
       "        [ 1.1654e+01],\n",
       "        [ 3.6623e+00],\n",
       "        [ 2.2067e+00],\n",
       "        [ 6.6879e+00],\n",
       "        [-4.4697e+00],\n",
       "        [ 3.3059e+00],\n",
       "        [ 1.3784e+00],\n",
       "        [ 1.4509e+00],\n",
       "        [ 1.8535e+00],\n",
       "        [ 5.8577e+00],\n",
       "        [ 7.8474e-02],\n",
       "        [ 3.4969e+00],\n",
       "        [ 6.0538e+00],\n",
       "        [ 8.9876e+00],\n",
       "        [ 9.5715e-01],\n",
       "        [ 5.9321e+00],\n",
       "        [ 8.9889e+00],\n",
       "        [ 4.7038e+00],\n",
       "        [ 6.9221e+00],\n",
       "        [-1.4921e+00],\n",
       "        [ 1.2131e+00],\n",
       "        [ 5.0565e+00],\n",
       "        [ 4.4022e+00],\n",
       "        [-9.6373e-01],\n",
       "        [ 3.4418e+00],\n",
       "        [ 7.8362e+00],\n",
       "        [ 8.0345e+00],\n",
       "        [ 3.5103e+00],\n",
       "        [-4.5949e-01],\n",
       "        [ 6.8135e+00],\n",
       "        [ 5.5415e+00],\n",
       "        [-1.0499e+00],\n",
       "        [ 8.4303e+00],\n",
       "        [ 5.0357e+00],\n",
       "        [ 5.4152e+00],\n",
       "        [ 2.8464e+00],\n",
       "        [ 3.3990e+00],\n",
       "        [ 3.6088e+00],\n",
       "        [ 8.7423e+00],\n",
       "        [ 1.4079e+00],\n",
       "        [-1.6320e+00],\n",
       "        [ 4.1228e+00],\n",
       "        [ 3.5407e+00],\n",
       "        [ 4.5336e+00],\n",
       "        [-2.3267e+00],\n",
       "        [ 7.1371e-01],\n",
       "        [ 7.8881e+00],\n",
       "        [ 1.5757e+00],\n",
       "        [ 2.3486e+00],\n",
       "        [ 5.8351e+00],\n",
       "        [ 3.5940e+00],\n",
       "        [-1.0888e+00],\n",
       "        [ 3.3260e+00],\n",
       "        [ 1.2665e+01],\n",
       "        [ 2.4408e+00],\n",
       "        [ 4.1770e+00],\n",
       "        [ 2.9282e+00],\n",
       "        [ 6.4580e+00],\n",
       "        [ 5.0715e+00],\n",
       "        [ 5.2079e+00],\n",
       "        [ 3.9695e+00],\n",
       "        [ 8.5055e+00],\n",
       "        [ 3.3973e+00],\n",
       "        [ 7.4693e+00],\n",
       "        [ 6.6574e+00],\n",
       "        [-1.1402e+00],\n",
       "        [ 3.9707e+00],\n",
       "        [-3.6318e-01],\n",
       "        [ 5.9220e-01],\n",
       "        [ 9.0638e+00],\n",
       "        [ 6.1155e+00],\n",
       "        [ 4.3470e+00],\n",
       "        [ 4.0063e+00],\n",
       "        [ 5.8720e+00],\n",
       "        [ 7.1946e-01],\n",
       "        [ 4.3824e+00],\n",
       "        [ 7.0319e+00],\n",
       "        [-1.5444e+00],\n",
       "        [ 4.9765e+00],\n",
       "        [ 5.0594e+00],\n",
       "        [ 3.4473e+00],\n",
       "        [ 5.4864e+00],\n",
       "        [ 3.7519e+00],\n",
       "        [-4.4880e+00],\n",
       "        [ 7.8424e+00],\n",
       "        [ 5.2182e+00],\n",
       "        [ 3.4852e+00],\n",
       "        [ 2.3142e+00],\n",
       "        [ 2.0429e+00],\n",
       "        [ 2.9217e+00],\n",
       "        [ 7.5724e+00],\n",
       "        [ 7.0407e+00],\n",
       "        [ 2.0352e+00],\n",
       "        [ 2.8623e+00],\n",
       "        [ 3.5728e+00],\n",
       "        [-2.6499e+00],\n",
       "        [ 6.7821e+00],\n",
       "        [ 3.1610e+00],\n",
       "        [ 8.3652e-01],\n",
       "        [ 2.0375e+00],\n",
       "        [-1.3323e+00],\n",
       "        [ 5.6883e+00],\n",
       "        [ 1.1888e+01],\n",
       "        [ 1.0051e+01],\n",
       "        [ 1.2694e+01],\n",
       "        [-1.5889e+00],\n",
       "        [-2.3536e-01],\n",
       "        [ 7.7180e-01],\n",
       "        [ 5.4110e+00],\n",
       "        [ 1.6889e+00],\n",
       "        [ 1.0972e+01],\n",
       "        [ 1.2894e+00],\n",
       "        [ 8.1284e+00],\n",
       "        [ 8.2850e+00],\n",
       "        [ 4.8311e+00],\n",
       "        [ 9.3773e+00],\n",
       "        [-4.2205e+00],\n",
       "        [ 1.0449e+01],\n",
       "        [ 4.0979e+00],\n",
       "        [-4.2159e-01],\n",
       "        [ 9.7423e+00],\n",
       "        [ 6.3295e+00],\n",
       "        [ 3.5842e+00],\n",
       "        [ 4.4172e+00],\n",
       "        [ 1.2927e+01],\n",
       "        [ 4.9741e+00],\n",
       "        [ 1.2375e+00],\n",
       "        [ 7.3815e+00],\n",
       "        [ 1.3545e-01],\n",
       "        [ 2.1718e+00],\n",
       "        [ 1.3305e-01],\n",
       "        [ 6.2228e+00],\n",
       "        [ 3.7318e+00],\n",
       "        [ 3.0681e+00],\n",
       "        [ 2.2250e+00],\n",
       "        [ 3.6151e+00],\n",
       "        [ 2.8752e+00],\n",
       "        [ 5.1743e+00],\n",
       "        [ 3.6696e+00],\n",
       "        [-8.1100e-01],\n",
       "        [ 3.5145e+00],\n",
       "        [ 2.1446e+00],\n",
       "        [ 6.1808e+00],\n",
       "        [-1.2467e+00],\n",
       "        [ 7.4459e+00],\n",
       "        [-1.3849e-01],\n",
       "        [ 5.5235e+00],\n",
       "        [ 3.5528e+00],\n",
       "        [ 3.1240e+00],\n",
       "        [ 4.3594e+00],\n",
       "        [ 5.7036e+00],\n",
       "        [ 1.0452e+01],\n",
       "        [ 5.0863e+00],\n",
       "        [ 7.3287e+00],\n",
       "        [ 3.7725e+00],\n",
       "        [ 5.7642e+00],\n",
       "        [ 2.1032e+00],\n",
       "        [ 1.7868e+00],\n",
       "        [-3.0200e-01],\n",
       "        [-2.7020e+00],\n",
       "        [ 2.2638e+00],\n",
       "        [ 2.4757e+00],\n",
       "        [ 2.1936e+00],\n",
       "        [-2.5682e+00],\n",
       "        [ 7.8995e+00],\n",
       "        [ 6.9835e+00],\n",
       "        [-1.1679e+00],\n",
       "        [ 6.7425e+00],\n",
       "        [ 3.5449e+00],\n",
       "        [ 2.6506e+00],\n",
       "        [ 5.4830e+00],\n",
       "        [ 3.0195e+00],\n",
       "        [ 6.5481e+00],\n",
       "        [-2.5182e+00],\n",
       "        [ 7.6436e+00],\n",
       "        [ 3.9198e+00],\n",
       "        [ 6.4788e-01],\n",
       "        [ 5.2769e+00],\n",
       "        [ 7.6143e+00],\n",
       "        [ 5.0850e+00],\n",
       "        [ 2.3438e+00],\n",
       "        [ 2.6413e+00],\n",
       "        [ 2.0749e+00],\n",
       "        [ 5.0401e+00],\n",
       "        [ 2.9842e+00],\n",
       "        [ 6.0389e+00],\n",
       "        [ 3.5064e+00],\n",
       "        [ 4.6822e+00],\n",
       "        [ 2.4914e+00],\n",
       "        [ 5.7996e+00],\n",
       "        [ 8.9254e+00],\n",
       "        [ 8.7356e+00],\n",
       "        [ 6.6755e+00],\n",
       "        [ 6.6836e+00],\n",
       "        [ 3.1574e+00],\n",
       "        [-9.2420e-01],\n",
       "        [ 4.4302e+00],\n",
       "        [ 1.1605e+01],\n",
       "        [ 8.7549e+00],\n",
       "        [ 6.6416e+00],\n",
       "        [ 3.4817e+00],\n",
       "        [ 4.8657e+00],\n",
       "        [ 4.5005e+00],\n",
       "        [ 4.4601e+00],\n",
       "        [ 4.0627e-01],\n",
       "        [ 9.0356e+00],\n",
       "        [ 6.7774e-01],\n",
       "        [ 4.6787e+00],\n",
       "        [ 7.8265e+00],\n",
       "        [ 4.4462e+00],\n",
       "        [ 4.6602e+00],\n",
       "        [ 6.7787e+00],\n",
       "        [ 3.0400e+00],\n",
       "        [ 7.5414e+00],\n",
       "        [-2.6460e+00],\n",
       "        [ 4.8609e+00],\n",
       "        [ 1.0781e+01],\n",
       "        [ 9.2148e+00],\n",
       "        [-8.7435e-01],\n",
       "        [ 8.4408e+00],\n",
       "        [ 3.7471e+00],\n",
       "        [ 9.3573e+00],\n",
       "        [-6.2431e-01],\n",
       "        [ 7.2707e+00],\n",
       "        [-1.2592e+00],\n",
       "        [ 3.6766e+00],\n",
       "        [ 9.2745e+00],\n",
       "        [ 7.5311e+00],\n",
       "        [ 5.9122e+00],\n",
       "        [ 7.2999e+00],\n",
       "        [ 1.9488e+00],\n",
       "        [ 5.0415e+00],\n",
       "        [ 5.1002e+00],\n",
       "        [-3.8670e-01],\n",
       "        [ 5.7571e-01],\n",
       "        [ 6.9131e+00],\n",
       "        [ 7.9409e+00],\n",
       "        [ 6.4824e+00],\n",
       "        [ 6.9334e+00],\n",
       "        [-2.5451e-01],\n",
       "        [ 2.1492e+00],\n",
       "        [ 4.1039e+00],\n",
       "        [ 5.4608e+00],\n",
       "        [ 4.5615e+00],\n",
       "        [ 1.0103e+01],\n",
       "        [ 3.0593e+00],\n",
       "        [ 4.9138e+00],\n",
       "        [ 9.9959e-01],\n",
       "        [ 6.1949e+00],\n",
       "        [ 1.3212e+00],\n",
       "        [ 4.4588e+00],\n",
       "        [ 4.8482e+00],\n",
       "        [ 8.7230e+00],\n",
       "        [ 7.5292e+00],\n",
       "        [ 4.5242e+00],\n",
       "        [ 3.9015e+00],\n",
       "        [-2.4500e+00],\n",
       "        [ 1.1725e+01],\n",
       "        [ 1.0457e+01],\n",
       "        [ 5.0413e+00],\n",
       "        [ 8.3769e+00],\n",
       "        [ 5.8959e+00],\n",
       "        [ 7.5582e-01],\n",
       "        [ 8.6637e+00],\n",
       "        [ 5.5706e+00],\n",
       "        [ 5.7388e+00],\n",
       "        [ 2.6533e+00],\n",
       "        [-1.5460e+00],\n",
       "        [ 1.0070e+01],\n",
       "        [ 8.0433e+00],\n",
       "        [ 5.2758e+00],\n",
       "        [ 2.4416e+00],\n",
       "        [ 6.0427e+00],\n",
       "        [-2.3276e+00],\n",
       "        [ 5.7937e+00],\n",
       "        [ 3.6543e+00],\n",
       "        [ 1.3239e+01],\n",
       "        [ 2.0434e+00],\n",
       "        [-1.1718e+00],\n",
       "        [ 3.7923e+00],\n",
       "        [ 9.7954e+00],\n",
       "        [-4.5365e+00],\n",
       "        [-3.4864e+00],\n",
       "        [ 5.2704e+00],\n",
       "        [ 5.2080e+00],\n",
       "        [-5.4953e-01],\n",
       "        [-6.7971e-01],\n",
       "        [ 6.4400e+00],\n",
       "        [ 7.3222e+00],\n",
       "        [ 7.0629e+00],\n",
       "        [ 3.1305e+00],\n",
       "        [ 1.2912e+00],\n",
       "        [-1.9564e+00],\n",
       "        [ 3.6014e-01],\n",
       "        [ 7.2269e+00],\n",
       "        [ 1.1733e+01],\n",
       "        [ 1.0958e+01],\n",
       "        [ 7.9106e+00],\n",
       "        [ 4.3110e-01],\n",
       "        [ 3.2302e+00],\n",
       "        [ 1.0845e+01],\n",
       "        [ 4.6100e+00],\n",
       "        [ 1.2605e+01],\n",
       "        [ 2.9931e+00],\n",
       "        [ 1.3040e+01],\n",
       "        [ 5.1547e+00],\n",
       "        [-1.1587e+00],\n",
       "        [ 4.5332e+00],\n",
       "        [ 5.3799e+00],\n",
       "        [ 3.6722e+00],\n",
       "        [ 6.5322e+00],\n",
       "        [ 5.8788e-02],\n",
       "        [ 8.7148e+00],\n",
       "        [ 1.1374e+01],\n",
       "        [ 2.6757e-01],\n",
       "        [-3.8117e-01],\n",
       "        [ 3.8832e+00],\n",
       "        [ 1.4712e+00],\n",
       "        [ 4.2081e+00],\n",
       "        [ 9.0283e+00],\n",
       "        [-5.3166e+00],\n",
       "        [ 7.2141e+00],\n",
       "        [ 6.7244e+00],\n",
       "        [ 6.2025e+00],\n",
       "        [ 8.9953e+00],\n",
       "        [ 5.1278e+00],\n",
       "        [-4.5627e+00],\n",
       "        [ 4.6736e+00],\n",
       "        [ 9.2194e+00],\n",
       "        [ 1.3319e+00],\n",
       "        [ 1.8251e+00],\n",
       "        [ 6.9831e+00],\n",
       "        [ 1.3574e+01],\n",
       "        [ 1.0766e+01],\n",
       "        [-7.6973e-01],\n",
       "        [ 3.7127e+00],\n",
       "        [ 2.2876e+00],\n",
       "        [ 1.0304e+01],\n",
       "        [ 2.7314e+00],\n",
       "        [ 5.5949e+00],\n",
       "        [ 7.5492e-01],\n",
       "        [ 2.4844e+00],\n",
       "        [ 8.6990e+00],\n",
       "        [ 9.6414e-01],\n",
       "        [ 2.9622e+00],\n",
       "        [ 6.2399e+00],\n",
       "        [ 4.0895e+00],\n",
       "        [ 5.4033e+00],\n",
       "        [ 9.9993e-01],\n",
       "        [ 3.4940e+00],\n",
       "        [ 6.1202e-01],\n",
       "        [ 8.4523e+00],\n",
       "        [ 5.3716e+00],\n",
       "        [ 4.8846e+00],\n",
       "        [ 5.2706e+00],\n",
       "        [ 9.3890e-01],\n",
       "        [ 1.2510e+01],\n",
       "        [ 6.3819e+00],\n",
       "        [-8.4153e-01],\n",
       "        [ 1.0544e+01],\n",
       "        [-2.5334e+00],\n",
       "        [-4.5983e+00],\n",
       "        [ 1.3258e+01],\n",
       "        [ 5.0494e+00],\n",
       "        [ 4.2609e+00],\n",
       "        [ 7.9620e+00],\n",
       "        [ 2.5324e+00],\n",
       "        [ 1.0757e+01],\n",
       "        [ 1.5872e+00],\n",
       "        [ 3.6144e+00],\n",
       "        [ 4.7385e-01],\n",
       "        [ 1.8422e+00],\n",
       "        [ 3.9775e+00],\n",
       "        [ 6.2525e+00],\n",
       "        [ 1.1195e+01],\n",
       "        [ 6.1240e+00],\n",
       "        [ 4.6293e+00],\n",
       "        [ 3.0752e+00],\n",
       "        [ 7.2782e-01],\n",
       "        [-7.8158e-01],\n",
       "        [ 1.2188e+00],\n",
       "        [ 4.0219e+00],\n",
       "        [ 3.7309e+00],\n",
       "        [ 3.7059e+00],\n",
       "        [ 1.2141e+00],\n",
       "        [ 8.4495e+00],\n",
       "        [ 6.8692e+00],\n",
       "        [ 2.3449e+00],\n",
       "        [-5.3567e+00],\n",
       "        [ 6.6554e+00],\n",
       "        [ 1.9024e+00],\n",
       "        [ 5.9914e+00],\n",
       "        [ 6.8959e+00],\n",
       "        [ 6.4197e-01],\n",
       "        [ 2.1781e+00],\n",
       "        [-1.8018e+00],\n",
       "        [ 3.0748e+00],\n",
       "        [ 3.8789e+00],\n",
       "        [ 4.8997e+00],\n",
       "        [ 4.2880e+00],\n",
       "        [ 4.5406e-01],\n",
       "        [ 8.6938e+00],\n",
       "        [ 5.9364e+00],\n",
       "        [ 3.0311e+00],\n",
       "        [ 4.6172e+00],\n",
       "        [ 3.2979e+00],\n",
       "        [ 4.9384e+00],\n",
       "        [ 1.2209e+01],\n",
       "        [ 3.5640e+00],\n",
       "        [-2.6827e-01],\n",
       "        [ 6.5121e+00],\n",
       "        [ 8.6102e+00],\n",
       "        [ 4.4953e+00],\n",
       "        [ 4.3712e+00],\n",
       "        [ 7.3023e+00],\n",
       "        [ 2.5489e+00],\n",
       "        [ 6.8046e+00],\n",
       "        [ 1.1235e+00],\n",
       "        [ 3.5923e+00],\n",
       "        [-3.2794e-01],\n",
       "        [-1.5748e-01],\n",
       "        [ 3.7005e+00],\n",
       "        [ 1.1326e+01],\n",
       "        [-2.4891e+00],\n",
       "        [ 2.2300e+00],\n",
       "        [ 7.3018e+00],\n",
       "        [ 8.5975e+00],\n",
       "        [ 7.9230e+00],\n",
       "        [ 7.3106e-01],\n",
       "        [-8.6126e-01],\n",
       "        [ 2.9566e+00],\n",
       "        [-3.2404e+00],\n",
       "        [ 4.2347e+00],\n",
       "        [ 2.9552e+00],\n",
       "        [ 3.4059e+00],\n",
       "        [ 2.0114e+00],\n",
       "        [ 7.1662e+00],\n",
       "        [ 4.8220e+00],\n",
       "        [ 9.2347e+00],\n",
       "        [ 1.6847e+00],\n",
       "        [ 5.4647e+00],\n",
       "        [ 5.7449e+00],\n",
       "        [ 5.9447e+00],\n",
       "        [ 3.0493e+00],\n",
       "        [ 1.2058e+01],\n",
       "        [ 2.1263e+00],\n",
       "        [ 6.3507e+00],\n",
       "        [ 6.5795e+00],\n",
       "        [-2.3220e+00],\n",
       "        [ 5.9939e+00],\n",
       "        [ 1.3776e+01],\n",
       "        [ 4.6465e+00],\n",
       "        [ 2.2363e-01],\n",
       "        [ 9.0363e+00],\n",
       "        [ 7.4043e+00],\n",
       "        [ 2.8180e+00],\n",
       "        [ 6.5911e+00],\n",
       "        [ 3.9087e+00],\n",
       "        [ 2.3714e+00],\n",
       "        [ 2.1910e+00],\n",
       "        [ 3.1656e+00],\n",
       "        [ 5.2050e+00],\n",
       "        [ 5.7134e+00],\n",
       "        [ 5.0324e+00],\n",
       "        [ 5.4182e+00],\n",
       "        [ 3.6407e+00],\n",
       "        [ 7.6073e+00],\n",
       "        [ 3.3834e+00],\n",
       "        [ 2.7113e+00],\n",
       "        [-3.8391e+00],\n",
       "        [ 6.5509e-01],\n",
       "        [ 6.8178e+00],\n",
       "        [ 4.3804e+00],\n",
       "        [ 8.1245e+00],\n",
       "        [ 3.4716e-01],\n",
       "        [ 7.9299e-01],\n",
       "        [ 5.9922e+00],\n",
       "        [ 4.9031e+00],\n",
       "        [ 3.2929e+00],\n",
       "        [ 2.2021e+00],\n",
       "        [ 7.0925e+00],\n",
       "        [ 6.4817e+00],\n",
       "        [ 5.9020e+00],\n",
       "        [ 2.5171e+00],\n",
       "        [ 8.0419e+00],\n",
       "        [ 1.6860e+01],\n",
       "        [-4.5866e+00],\n",
       "        [ 7.1648e+00],\n",
       "        [ 7.2638e+00],\n",
       "        [ 5.2188e+00],\n",
       "        [ 1.3642e+00],\n",
       "        [ 4.8736e+00],\n",
       "        [ 5.3096e+00],\n",
       "        [ 1.0360e+01],\n",
       "        [ 7.8608e+00],\n",
       "        [ 2.2020e-01],\n",
       "        [-1.1615e+00],\n",
       "        [ 2.6482e+00],\n",
       "        [ 3.4120e+00],\n",
       "        [-2.0799e+00],\n",
       "        [ 1.4575e+00],\n",
       "        [ 8.6568e+00],\n",
       "        [ 9.6760e+00],\n",
       "        [ 1.2161e+01],\n",
       "        [-5.5941e-01],\n",
       "        [ 4.3351e+00],\n",
       "        [ 1.4534e+00],\n",
       "        [ 5.2479e+00],\n",
       "        [ 1.1288e+01],\n",
       "        [ 3.7384e+00],\n",
       "        [ 2.9066e+00],\n",
       "        [ 5.9611e+00],\n",
       "        [-4.1411e-01],\n",
       "        [ 5.1143e+00],\n",
       "        [ 3.7718e+00],\n",
       "        [ 7.6801e-01],\n",
       "        [ 8.6319e+00],\n",
       "        [ 6.6639e+00],\n",
       "        [ 3.1879e+00],\n",
       "        [ 4.4363e+00],\n",
       "        [ 2.1442e+00],\n",
       "        [ 1.8622e+00],\n",
       "        [ 4.0631e+00],\n",
       "        [ 1.7766e+00],\n",
       "        [ 5.4000e+00],\n",
       "        [ 5.4395e+00],\n",
       "        [ 7.1115e+00],\n",
       "        [ 2.3203e+00],\n",
       "        [-3.4123e-01],\n",
       "        [-1.8512e+00],\n",
       "        [-2.6183e+00],\n",
       "        [ 1.4175e+01],\n",
       "        [ 3.9628e+00],\n",
       "        [-2.0723e+00],\n",
       "        [ 2.1722e+00],\n",
       "        [ 5.1867e+00],\n",
       "        [-3.0778e-01],\n",
       "        [ 6.6740e+00],\n",
       "        [ 2.1267e+00],\n",
       "        [-1.9793e+00],\n",
       "        [ 4.1680e+00],\n",
       "        [ 6.1715e+00],\n",
       "        [-1.3346e+00],\n",
       "        [ 2.6796e+00],\n",
       "        [ 4.1780e+00],\n",
       "        [ 7.4616e+00],\n",
       "        [ 1.8810e+00],\n",
       "        [ 2.4950e+00],\n",
       "        [ 8.0986e+00],\n",
       "        [ 2.4401e+00],\n",
       "        [ 2.9262e+00],\n",
       "        [ 5.4145e+00],\n",
       "        [ 3.1029e+00],\n",
       "        [ 3.3823e+00],\n",
       "        [ 2.6419e+00],\n",
       "        [ 8.4842e+00],\n",
       "        [ 6.9789e+00],\n",
       "        [-2.8460e+00],\n",
       "        [ 1.2922e+01],\n",
       "        [ 1.1573e+01],\n",
       "        [ 7.2769e-01],\n",
       "        [ 4.7487e+00],\n",
       "        [ 4.1401e+00],\n",
       "        [ 4.9666e+00],\n",
       "        [ 1.6299e-01],\n",
       "        [ 9.6387e-01],\n",
       "        [ 6.4571e+00],\n",
       "        [ 1.0600e+01],\n",
       "        [ 9.7201e+00],\n",
       "        [ 3.1920e+00],\n",
       "        [ 4.0489e+00],\n",
       "        [ 3.0434e+00],\n",
       "        [ 5.9668e+00],\n",
       "        [ 4.3529e+00],\n",
       "        [ 1.0592e-02],\n",
       "        [ 4.6311e+00],\n",
       "        [ 4.2742e+00],\n",
       "        [-7.4355e+00],\n",
       "        [-6.8650e-02],\n",
       "        [ 1.2828e+00],\n",
       "        [ 7.6095e+00],\n",
       "        [ 5.0395e+00],\n",
       "        [ 3.8900e+00],\n",
       "        [ 5.9383e+00],\n",
       "        [ 3.7077e+00],\n",
       "        [ 1.0981e+01],\n",
       "        [ 7.8156e-01],\n",
       "        [ 5.1753e+00],\n",
       "        [ 8.0415e+00],\n",
       "        [ 5.4110e+00],\n",
       "        [ 8.4481e+00],\n",
       "        [ 1.4116e+00],\n",
       "        [ 4.8352e+00],\n",
       "        [ 4.1651e+00],\n",
       "        [ 4.4527e-01],\n",
       "        [ 1.0967e+01],\n",
       "        [ 3.4359e+00],\n",
       "        [ 2.4634e+00],\n",
       "        [ 4.0570e+00],\n",
       "        [ 1.0724e+01],\n",
       "        [ 3.6414e+00],\n",
       "        [ 4.5376e-02],\n",
       "        [ 2.4827e+00],\n",
       "        [ 1.0235e+01],\n",
       "        [ 2.2121e+00],\n",
       "        [ 8.6818e-01],\n",
       "        [ 4.1139e+00],\n",
       "        [ 2.6940e+00],\n",
       "        [ 7.4263e+00],\n",
       "        [ 1.3099e+00],\n",
       "        [ 5.5389e-02],\n",
       "        [ 2.1359e+00],\n",
       "        [ 5.5367e+00],\n",
       "        [-2.7497e-01],\n",
       "        [ 1.8004e+00],\n",
       "        [-3.1964e-01],\n",
       "        [ 7.8663e+00],\n",
       "        [-6.4964e-01],\n",
       "        [ 7.8653e+00],\n",
       "        [ 6.1220e+00],\n",
       "        [ 2.3049e+00],\n",
       "        [ 6.7934e+00],\n",
       "        [-1.3133e+00],\n",
       "        [ 1.7485e+00],\n",
       "        [ 6.7526e+00],\n",
       "        [ 3.0870e+00],\n",
       "        [ 1.2593e+00],\n",
       "        [ 2.7472e+00],\n",
       "        [ 3.6517e+00],\n",
       "        [ 7.2607e+00],\n",
       "        [-2.5410e-01],\n",
       "        [-3.9745e+00],\n",
       "        [ 8.3329e-01],\n",
       "        [ 3.8494e+00],\n",
       "        [ 1.1582e+01],\n",
       "        [ 7.3500e+00],\n",
       "        [ 5.3138e+00],\n",
       "        [ 4.3310e+00],\n",
       "        [-1.1099e+00],\n",
       "        [ 7.0139e+00],\n",
       "        [ 1.4940e+00],\n",
       "        [ 8.9377e+00],\n",
       "        [ 8.2362e+00],\n",
       "        [-2.5547e+00],\n",
       "        [ 8.1350e+00],\n",
       "        [-5.9852e+00],\n",
       "        [ 3.6315e+00],\n",
       "        [-4.8937e-01],\n",
       "        [ 1.4372e+01],\n",
       "        [ 5.1491e+00],\n",
       "        [ 9.9742e+00],\n",
       "        [ 3.2629e+00],\n",
       "        [ 8.6775e+00],\n",
       "        [ 2.2400e+00],\n",
       "        [ 5.8731e+00],\n",
       "        [-4.3250e-01],\n",
       "        [-2.6975e-01],\n",
       "        [ 2.4177e+00],\n",
       "        [-5.1719e+00],\n",
       "        [ 6.6326e+00],\n",
       "        [ 1.3314e+00],\n",
       "        [ 5.7487e-01],\n",
       "        [ 8.8210e+00],\n",
       "        [ 1.1096e+01],\n",
       "        [ 1.6895e+00],\n",
       "        [ 5.2405e+00],\n",
       "        [ 2.6807e+00],\n",
       "        [ 5.2575e+00],\n",
       "        [ 6.2433e+00],\n",
       "        [ 2.6265e+00],\n",
       "        [ 1.8446e+00],\n",
       "        [ 1.0151e+01],\n",
       "        [ 5.8531e+00],\n",
       "        [ 3.3234e+00],\n",
       "        [-2.3675e+00],\n",
       "        [ 3.3620e+00],\n",
       "        [ 8.3756e+00],\n",
       "        [ 4.6769e+00],\n",
       "        [ 6.7349e+00],\n",
       "        [-4.4628e+00],\n",
       "        [-5.0479e+00],\n",
       "        [ 1.0048e+01],\n",
       "        [ 2.8907e-01],\n",
       "        [ 4.7761e+00],\n",
       "        [ 2.5690e+00],\n",
       "        [ 5.6276e+00],\n",
       "        [ 3.8694e+00],\n",
       "        [ 7.8495e+00],\n",
       "        [ 6.2752e+00],\n",
       "        [ 8.1467e+00],\n",
       "        [ 5.4259e+00],\n",
       "        [ 4.5105e+00],\n",
       "        [ 8.1461e+00],\n",
       "        [ 7.2161e+00],\n",
       "        [ 1.5731e+00],\n",
       "        [ 5.5032e+00],\n",
       "        [ 1.1892e+01],\n",
       "        [ 6.3820e+00],\n",
       "        [ 1.1433e+01],\n",
       "        [ 6.9966e+00],\n",
       "        [ 6.3790e+00],\n",
       "        [ 4.3702e+00],\n",
       "        [ 2.0478e-01],\n",
       "        [ 9.6385e+00],\n",
       "        [ 7.9146e+00],\n",
       "        [ 9.3894e+00],\n",
       "        [-6.8909e-01],\n",
       "        [ 8.3598e+00],\n",
       "        [-2.5899e+00],\n",
       "        [-9.3731e-01],\n",
       "        [ 1.0549e+01],\n",
       "        [ 7.5469e+00],\n",
       "        [-1.3847e+00],\n",
       "        [ 3.8296e+00],\n",
       "        [ 5.0489e-01],\n",
       "        [ 6.5512e+00],\n",
       "        [ 3.2879e+00],\n",
       "        [ 1.2045e+01],\n",
       "        [ 2.7369e+00],\n",
       "        [ 3.8811e+00],\n",
       "        [ 1.1910e+01],\n",
       "        [ 4.5464e+00],\n",
       "        [ 1.2185e+01],\n",
       "        [ 2.6951e+00],\n",
       "        [ 9.3023e+00],\n",
       "        [ 7.9136e+00],\n",
       "        [ 4.4160e+00],\n",
       "        [-4.9046e+00],\n",
       "        [ 6.8926e+00],\n",
       "        [ 7.3732e+00],\n",
       "        [ 1.3880e-01],\n",
       "        [ 4.4140e+00],\n",
       "        [ 2.5612e+00],\n",
       "        [ 2.0830e+00],\n",
       "        [ 5.7801e+00],\n",
       "        [ 6.7498e+00],\n",
       "        [ 1.0066e+01],\n",
       "        [ 5.2613e+00],\n",
       "        [ 7.2635e-01],\n",
       "        [-1.6449e+00],\n",
       "        [ 6.6702e+00],\n",
       "        [ 9.3229e+00],\n",
       "        [ 3.5075e+00],\n",
       "        [ 2.8902e+00],\n",
       "        [ 1.1902e+01],\n",
       "        [ 7.4615e+00],\n",
       "        [ 2.8934e+00],\n",
       "        [ 1.7570e+00],\n",
       "        [-1.7188e+00],\n",
       "        [ 1.5603e+00],\n",
       "        [-1.0429e+00],\n",
       "        [ 1.2803e+00],\n",
       "        [ 1.8802e+00],\n",
       "        [-4.6308e+00],\n",
       "        [ 3.9431e+00],\n",
       "        [ 5.4877e+00],\n",
       "        [ 1.0163e+01],\n",
       "        [ 6.6157e+00],\n",
       "        [ 9.0485e+00],\n",
       "        [ 3.0995e+00],\n",
       "        [ 4.5944e+00],\n",
       "        [ 3.6107e+00],\n",
       "        [ 5.0371e+00],\n",
       "        [ 2.4405e+00],\n",
       "        [ 4.2714e+00],\n",
       "        [ 9.2776e+00],\n",
       "        [-1.2730e+00],\n",
       "        [ 3.1365e+00],\n",
       "        [-1.6825e+00],\n",
       "        [ 8.9980e+00],\n",
       "        [ 1.2882e+00],\n",
       "        [ 3.9377e+00],\n",
       "        [-3.9052e-01],\n",
       "        [ 9.3019e+00],\n",
       "        [ 4.9169e+00],\n",
       "        [ 1.8159e+00],\n",
       "        [ 6.5036e+00],\n",
       "        [ 1.7808e+00],\n",
       "        [ 3.1341e-01],\n",
       "        [ 2.5525e+00],\n",
       "        [ 5.8957e+00],\n",
       "        [ 6.5631e+00],\n",
       "        [ 5.7969e+00],\n",
       "        [ 9.1198e-01],\n",
       "        [ 5.2363e+00],\n",
       "        [ 6.5884e+00],\n",
       "        [ 9.2498e+00],\n",
       "        [ 1.4551e+00],\n",
       "        [ 5.7751e+00],\n",
       "        [ 4.6552e+00],\n",
       "        [ 5.1161e+00],\n",
       "        [-1.8128e+00],\n",
       "        [ 9.9549e+00],\n",
       "        [ 8.3233e-01],\n",
       "        [ 2.1752e+00],\n",
       "        [ 8.2447e+00],\n",
       "        [-3.3557e+00],\n",
       "        [ 2.8148e+00],\n",
       "        [ 9.0338e+00],\n",
       "        [-1.3105e+00],\n",
       "        [ 7.7855e+00],\n",
       "        [ 5.0858e+00],\n",
       "        [ 1.1867e+01],\n",
       "        [-5.6736e+00],\n",
       "        [ 1.1450e+00],\n",
       "        [ 1.6509e+00],\n",
       "        [ 8.7370e+00],\n",
       "        [ 5.4001e+00],\n",
       "        [ 1.5244e+00],\n",
       "        [-1.3970e+00],\n",
       "        [ 1.2492e-01],\n",
       "        [ 3.3264e+00],\n",
       "        [ 8.9290e+00],\n",
       "        [ 1.6610e+00],\n",
       "        [ 1.0382e+00],\n",
       "        [ 4.8715e+00],\n",
       "        [ 5.4528e+00],\n",
       "        [-2.4271e+00],\n",
       "        [ 2.5236e+00],\n",
       "        [-6.5123e+00],\n",
       "        [ 4.3397e+00],\n",
       "        [ 9.1537e+00],\n",
       "        [-1.9439e+00],\n",
       "        [ 3.2188e+00],\n",
       "        [ 4.5058e+00],\n",
       "        [ 8.9290e+00],\n",
       "        [ 4.1881e+00],\n",
       "        [ 5.0221e+00],\n",
       "        [ 7.6559e+00],\n",
       "        [-6.7393e-02],\n",
       "        [ 3.3512e+00],\n",
       "        [ 5.4479e+00],\n",
       "        [-5.0107e+00],\n",
       "        [ 7.7217e+00],\n",
       "        [ 6.2523e+00],\n",
       "        [ 6.2218e+00],\n",
       "        [-2.8131e-01],\n",
       "        [ 7.2481e-01],\n",
       "        [ 3.6819e+00],\n",
       "        [ 5.5112e+00],\n",
       "        [ 3.6475e+00],\n",
       "        [ 7.6532e-01],\n",
       "        [ 3.4722e+00],\n",
       "        [ 4.9060e+00],\n",
       "        [ 3.6283e+00],\n",
       "        [-1.2323e+00],\n",
       "        [ 3.5793e+00],\n",
       "        [ 2.2878e-01],\n",
       "        [ 6.6933e+00],\n",
       "        [ 1.1880e+01],\n",
       "        [ 8.8079e+00],\n",
       "        [ 3.2515e+00],\n",
       "        [-2.1293e-01]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-08T02:00:56.896335Z",
     "start_time": "2025-09-08T02:00:56.893943Z"
    }
   },
   "cell_type": "code",
   "source": "X.shape, Y.shape",
   "id": "b3462d859f2e5d62",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(torch.Size([1000, 2]), torch.Size([1000]))"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-08T02:00:56.995185Z",
     "start_time": "2025-09-08T02:00:56.989962Z"
    }
   },
   "cell_type": "code",
   "source": "print(f'features: {X[0]}, labels: {Y[0]}')",
   "id": "4b86057daec9235c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "features: tensor([-0.5402, -0.8185]), labels: 5.905240535736084\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-08T02:00:57.084185Z",
     "start_time": "2025-09-08T02:00:57.072954Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import random\n",
    "\n",
    "\n",
    "# 小批量读取数据\n",
    "def data_iter(batch_size, features, labels):\n",
    "    n = len(features)\n",
    "    indices = list(range(n))\n",
    "    random.shuffle(indices)\n",
    "    for i in range(0, n, batch_size):\n",
    "        batch_indices = torch.tensor(indices[i: min(i + batch_size, n)])\n",
    "        yield features[batch_indices], labels[batch_indices]"
   ],
   "id": "eb4156b77f91eea",
   "outputs": [],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-08T02:00:57.101738Z",
     "start_time": "2025-09-08T02:00:57.092165Z"
    }
   },
   "cell_type": "code",
   "source": [
    "batch_size = 10\n",
    "for features, labels in data_iter(batch_size, X, Y):\n",
    "    print(features, \"\\n\", labels)\n",
    "    break"
   ],
   "id": "7407469b49e9d746",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 2.0923,  2.2397],\n",
      "        [-0.1528, -0.6354],\n",
      "        [-2.2849,  0.6139],\n",
      "        [-0.8908,  2.2735],\n",
      "        [ 1.5168,  0.6294],\n",
      "        [ 0.0967,  0.4590],\n",
      "        [-1.3045,  0.3252],\n",
      "        [-0.0801,  0.0370],\n",
      "        [-0.8199, -0.0530],\n",
      "        [-1.0397,  0.4962]]) \n",
      " tensor([ 0.7867,  6.0547, -2.4500, -5.3166,  5.1002,  2.8464,  0.4598,  3.9198,\n",
      "         2.7472,  0.4541])\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-08T02:00:57.153269Z",
     "start_time": "2025-09-08T02:00:57.131792Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def linereg(X, w, b):\n",
    "    return torch.mm(X, w) + b\n",
    "\n",
    "def squared_loss(y_hat, y):\n",
    "    return (y_hat - y.reshape(y_hat.shape)) ** 2 / 2\n",
    "\n",
    "def sgd(params, lr, batch_size):\n",
    "    with torch.no_grad():\n",
    "        for param in params:\n",
    "            param -= lr * param.grad / batch_size\n",
    "            param.grad.zero_()"
   ],
   "id": "89ded44891ede985",
   "outputs": [],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-08T02:14:22.678822Z",
     "start_time": "2025-09-08T02:14:22.636789Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 初始化参数\n",
    "# w = torch.normal(0, 0.01, size=(2, 1), requires_grad=True)\n",
    "w = torch.zeros(size=(2, 1), requires_grad=True)\n",
    "b = torch.zeros(size=(1, ), requires_grad=True)\n",
    "\n",
    "# 超参数\n",
    "lr = 0.03\n",
    "batch_size = 10\n",
    "epoch = 3\n",
    "net = linereg\n",
    "loss = squared_loss\n",
    "for epoch in range(epoch):\n",
    "    for features, labels in data_iter(batch_size, X, Y):\n",
    "        l = loss(net(features, w, b), labels)\n",
    "        l.sum().backward()\n",
    "        sgd([w, b], lr, batch_size)\n",
    "\n",
    "    with torch.no_grad():\n",
    "       train_l = loss(net(X, w, b), Y)\n",
    "       print(f'epoch: {epoch}, train_l: {float(train_l.mean()): f}')\n"
   ],
   "id": "c836d3ea4978f93f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch: 0, train_l:  0.039985\n",
      "epoch: 1, train_l:  0.000150\n",
      "epoch: 2, train_l:  0.000047\n"
     ]
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-08T02:14:25.542574Z",
     "start_time": "2025-09-08T02:14:25.536208Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(f'w的估计误差: {true_w - w.reshape(true_w.shape)}')\n",
    "print(f'b的估计误差: {true_b - b}')"
   ],
   "id": "5b8bb1368b3a2aa8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w的估计误差: tensor([ 0.0004, -0.0003], grad_fn=<SubBackward0>)\n",
      "b的估计误差: tensor([0.0011], grad_fn=<SubBackward0>)\n"
     ]
    }
   ],
   "execution_count": 35
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
